﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using caedisi.Web.DataModel;
using caedisi.Controller;

namespace caedisi.Views
{
    public partial class CellularAutomataSearchFilter : ChildWindow, INotifyPropertyChanged
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CellularAutomataSearchFilter"/> class.
        /// </summary>
        public CellularAutomataSearchFilter()
        {
            InitializeComponent();

            CellularAutomataController.Instance.LazyLoadAllCellularAutomata(cas =>
                {
                    if (cas != null)
                    {
                        this.CellularAutomatas = new ObservableCollection<CellularAutomata>(cas);

                        if (CellularAutomatas.Count>0)
                            this.dataGridCas.SelectedItem = this.CellularAutomatas[0];
                    }
                });
        
        }

        /// <summary>
        /// Handles the Click event of the OKButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        /// <summary>
        /// Handles the Click event of the CancelButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Gets or sets the selected cellular automata id.
        /// </summary>
        /// <value>
        /// The selected cellular automata id.
        /// </value>
        public int SelectedCellularAutomataId 
        {
            get
            {
                CellularAutomata ca = this.dataGridCas.SelectedItem as CellularAutomata;

                return ca.CellularAutomataId;
            }
        
        }

        private ObservableCollection<CellularAutomata> _CellularAutomatas;
        /// <summary>
        /// Gets or sets the current cellular automata.
        /// </summary>
        /// <value>
        /// The current cellular automata.
        /// </value>
        public ObservableCollection<CellularAutomata> CellularAutomatas
        {
            get
            {
                return _CellularAutomatas;
            }
            set
            {
                if (_CellularAutomatas != value)
                {
                    _CellularAutomatas = value;
                    this.RaisePropertyChanged("CellularAutomatas");

                }
            }
        }

        #region INotifiedPropertyChange
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        
    }
}

